package com.example.mypplication.component


import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.motion.widget.MotionLayout
import com.example.mypplication.R

class Component4Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_component4)

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)

        // 设置适配器
        val items = List(50) { index -> "Item ${index + 1}" }
        recyclerView.adapter = SimpleAdapter(items)

        val motionLayout = findViewById<MotionLayout>(R.id.motionLayout)

        // 添加滚动监听器
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                // 如果列表在顶部且向下滑动，确保状态是展开的
                if (!recyclerView.canScrollVertically(-1)) {
                        if (dy < 0 && motionLayout.progress > 0) {
                            motionLayout.transitionToState(R.id.expanded)
                        }
                    }
            }
        })

        // 监听MotionLayout的过渡完成事件
        motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
            override fun onTransitionStarted(motionLayout: MotionLayout, startId: Int, endId: Int) {}

            override fun onTransitionChange(motionLayout: MotionLayout, startId: Int, endId: Int, progress: Float) {
                // 动态调整RecyclerView的paddingTop，确保内容不被遮挡
                val stickyHeight = findViewById<View>(R.id.stickyHeader).height
                recyclerView.setPadding(
                    recyclerView.paddingLeft,
                    (stickyHeight * (1 - progress)).toInt(),
                    recyclerView.paddingRight,
                    recyclerView.paddingBottom
                )
            }

            override fun onTransitionCompleted(motionLayout: MotionLayout, currentId: Int) {}
            override fun onTransitionTrigger(motionLayout: MotionLayout, triggerId: Int, positive: Boolean, progress: Float) {}
        })
    }

    private inner class SimpleAdapter(private val items: List<String>) :
        RecyclerView.Adapter<SimpleAdapter.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(android.R.layout.simple_list_item_1, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.textView.text = items[position]
        }

        override fun getItemCount(): Int = items.size

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val textView: TextView = itemView.findViewById(android.R.id.text1)
        }
    }
}